package com.mxchip.livestar.ui.call


import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.mxchip.livestar.R
import com.mxchip.livestar.base.SystemData
import com.mxchip.livestar.ui.ItemHolder
import kotlinx.android.synthetic.main.item_admin_user.*

class AdminAdapter(private val onClickListener: ((View, AVUser) -> Unit)? = null) : RecyclerView.Adapter<ItemHolder>() {

    var data: List<AVUser> = emptyList()

    override fun getItemCount(): Int {
        return data.size
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemHolder {
        val view = LayoutInflater.from(parent.context)
                .inflate(R.layout.item_admin_user, parent, false)
        return ItemHolder(view)
    }

    override fun onBindViewHolder(holder: ItemHolder, position: Int) {

        val user = data[position]

        val shortId = user.term.termId.trimStart('0')

        // val arrays = user.term.split("T")

        /*val shortId = if(arrays.size > 1) {
            arrays[1].trimStart('0')
        } else {
            user.term
        }*/


        if(user.term.roomId == SystemData.roomId() &&  user.term.termId == SystemData.userId()) {
            holder.number.text = "$shortId（本机）"
        } else {
            holder.number.text = shortId

        }

        holder.name.text = user.name


        holder.text_mic.visibility =  if(user.micEnabled) View.VISIBLE else View.GONE
        holder.text_camera.visibility =  if(user.cameraLocalEnabled) View.VISIBLE else View.GONE
        // holder.text_hdmi.visibility =  if(user.cameraHdmiEnabled) View.VISIBLE else View.GONE

        holder.containerView.setOnClickListener {
            onClickListener?.invoke(it, user)
        }
    }

}